package com.shire.movienizerdroid.domain;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.database.Cursor;

import com.shire.movienizerdroid.db.DataSource;

/**
 * @author yhernandezdiaz
 */
public class Movie implements Serializable {
	private static final long serialVersionUID = 3455029868371237105L;

	public static final String COLUMN_TITLE = "title";
	public static final String COLUMN_YEAR = "year";
	public static final String COLUMN_DURATION = "duration";
	public static final String COLUMN_DESCRIPTION = "description";
	public static final String COLUMN_TAGLINE = "tagline";
	public static final String COLUMN_IMAGE_PATH = "image_path";
	public static final String COLUMN_IMAGE_URL = "image_url";
	public static final String COLUMN_USER_RATING = "rating";
	public static final String COLUMN_IMDB_SITE = "imdb_site";
	public static final String COLUMN_IMDB_RATING = "imdb_rating";
	public static final String COLUMN_IN_COLLECTION = "in_collection";
	public static final String COLUMN_SEEN = "seen";
	public static final String COLUMN_WANTED = "wanted";
	public static final String COLUMN_FOR_SALE = "for_sale";

	private final long code;
	private final String title;

	private final Map<String, Object> attributes = new HashMap<String, Object>();
	private final List<String> genres = new LinkedList<String>();
	private final List<String> countries = new LinkedList<String>();

	private final List<String> directors = new LinkedList<String>();
	private final List<String> screenplay = new LinkedList<String>();
	private final List<String> composers = new LinkedList<String>();
	private final List<String> actors = new LinkedList<String>();

	public Movie(Cursor cursor) {
		super();
		this.code = cursor.getLong(0);
		this.title = cursor.getString(1);

		if (cursor.getColumnCount() > 2) {
			final String[] columnNames = cursor.getColumnNames();
			for (int i = 2; i < columnNames.length; i++) {
				attributes.put(columnNames[i], cursor.getString(i));
			}
		}

		final Cursor movieManuals = DataSource.getInstance().getMovieManuals(code);
		while (movieManuals.moveToNext()) {
			switch ((int) movieManuals.getLong(0)) {
				case 8:
					genres.add(movieManuals.getString(1));
					break;
				case 9:
					countries.add(movieManuals.getString(1));
					break;
			}
		}
		movieManuals.close();
		final Cursor moviePersons = DataSource.getInstance().getMoviePersons(code);
		while (moviePersons.moveToNext()) {
			switch ((int) moviePersons.getLong(0)) {
				case 1:
					directors.add(moviePersons.getString(1));
					break;
				case 2:
					screenplay.add(moviePersons.getString(1));
					break;
				case 3:
					actors.add(moviePersons.getString(1));
					break;
				case 4:
					composers.add(moviePersons.getString(1));
					break;
			}
		}
		moviePersons.close();
	}

	public long getCode() {
		return code;
	}

	public String getTitle() {
		return title;
	}

	public int getYear() {
		return Integer.parseInt((String) attributes.get(COLUMN_YEAR));
	}

	public int getDuration() {
		return Integer.parseInt((String) attributes.get(COLUMN_DURATION));
	}

	public String getDescription() {
		return (String) attributes.get(COLUMN_DESCRIPTION);
	}

	public String getTagline() {
		return (String) attributes.get(COLUMN_TAGLINE);
	}

	public String getImagePath() {
		return (String) attributes.get(COLUMN_IMAGE_PATH);
	}

	public String getImageUrl() {
		return (String) attributes.get(COLUMN_IMAGE_URL);
	}

	public Float getUserRating() {
		try {
			return Float.parseFloat((String) attributes.get(COLUMN_USER_RATING));
		} catch (NumberFormatException e) {
			return 0f;
		} catch (NullPointerException e) {
			return 0f;
		}
	}

	public float getIMDBRating() {
		try {
			return Float.parseFloat((String) attributes.get(COLUMN_IMDB_RATING));
		} catch (NumberFormatException e) {
			return 0f;
		} catch (NullPointerException e) {
			return 0f;
		}
	}

	public String getIMDBSite() {
		return (String) attributes.get(COLUMN_IMDB_SITE);
	}

	public boolean isInCollection() {
		return !"0".equals(attributes.get(COLUMN_IN_COLLECTION));
	}

	public boolean isSeen() {
		return !"0".equals(attributes.get(COLUMN_SEEN));
	}

	public boolean isWanted() {
		return !"0".equals(attributes.get(COLUMN_WANTED));
	}

	public boolean isForSale() {
		return !"0".equals(attributes.get(COLUMN_FOR_SALE));
	}

	public List<String> getGenres() {
		return genres;
	}

	public List<String> getCountries() {
		return countries;
	}

	public List<String> getDirectors() {
		return directors;
	}

	public List<String> getScreenplay() {
		return screenplay;
	}

	public List<String> getComposers() {
		return composers;
	}

	public List<String> getActors() {
		return actors;
	}

}
